package com.gura.gura;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.os.Vibrator;
import android.view.WindowManager.LayoutParams;
import android.widget.Toast;

import com.badlogic.gdx.backends.android.AndroidApplication;
import com.guragura.Guragura;
import com.guragura.screens.GameScreen;
import com.guragura.screens.GuraScreen;
import com.guragura.util.GuraDialogInterface;
import com.guragura.util.GuraSettings;
import com.guragura.util.IGuraVibrator;


/**
 * @author hartono
 * GuraGura Activity adalah entry point untuk game ini.
 * Karena GuraGura mengextends {@link AndroidApplication} (Libgdx)
 * 
 * GuraGura Activity mengimplement dua interface
 * {@link GuraDialogInterface} dan {@link IGuraVibrator} yang merupakan penghubung antara
 * Android Activity - {@link Guragura}
 *
 */
public class GuraGuraActivity extends AndroidApplication implements
		com.guragura.util.GuraDialogInterface, IGuraVibrator {
	
	
	/**
	 * Core dari game guragura ini 
	 */
	public Guragura guragura;

	
	/**
	 * FLAG buat dialog -> {@link Activity#showDialog(int)}
	 */
	public static final int DIALOG_EXIT = 0;
	public static final int DIALOG_HELP = 1;
	public static final int DIALOG_GAME_MENU = 2;

	
	
	/**
	 * {@link Dialog} buat activity ini -> tapi ga kepake di game ini
	 */
	public Dialog exitDialog;
	public Dialog helpDialog;
	public Dialog gameDialog;

	
	/**
	 * Untuk mengaktifkan vibrasi / getaran pada handphone menggunakan kelas {@link Vibrator} 
	 */
	public Vibrator vibrator;

	
	/**
	 * Untuk melakukan komunikasi antara dua thread - UI Thread pada {@link Activity} dan Game Thread
	 * pada {@link Guragura} 
	 */
	protected Handler handler;

	
	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		// guragura = new Guragura(this);
		guragura = new Guragura(1, this);
		initialize(guragura, false);

		handler = new UIHandler(getMainLooper());

		// create exit dialog
		AlertDialog.Builder builder = new AlertDialog.Builder(this);
		builder.setMessage("Are you sure you want to exit?")
				.setCancelable(true)
				.setPositiveButton("Yes",
						new DialogInterface.OnClickListener() {
							public void onClick(DialogInterface dialog, int id) {
								guragura.dispose();
								System.exit(0);
							}
						})
				.setNegativeButton("No", new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int id) {
						dialog.cancel();
						guragura.mainMenuScreen.state = GuraScreen.SCREEN_RUNNING;
					}
				});
		builder.setOnCancelListener(new DialogInterface.OnCancelListener() {

			@Override
			public void onCancel(DialogInterface dialog) {
				// TODO Auto-generated method stub
				guragura.mainMenuScreen.state = GuraScreen.SCREEN_RUNNING;
			}
		});

		exitDialog = builder.create();

		// create help dialog
		helpDialog = new Dialog(this);
		helpDialog.setContentView(R.layout.help_dialog);
		helpDialog.setTitle("Help");
		helpDialog.setCancelable(true);
		helpDialog.setOnCancelListener(new DialogInterface.OnCancelListener() {

			@Override
			public void onCancel(DialogInterface dialog) {
				// TODO Auto-generated method stub
				guragura.sScreens.peek().state = GuraScreen.SCREEN_RUNNING;
			}
		});

		// menjaga agar screen tetap terjaga menyala terus
		getWindow().addFlags(LayoutParams.FLAG_KEEP_SCREEN_ON);
		
		// mendapatkan service Vibrator
		vibrator = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
	}

	
	// uncomment bila dibutuhkan, code dibawah memanfaatkan fasilitas android untuk membuat dialog sederhana
	
	// @Override
	// protected void onPrepareDialog(int id, Dialog dialog) {
	// // TODO Auto-generated method stub
	// switch (id) {
	// case DIALOG_EXIT: {
	// guragura.mainMenuScreen.state = GuraScreen.SCREEN_PAUSE;
	// break;
	// }
	// case DIALOG_HELP: {
	// guragura.sScreens.peek().state = GuraScreen.SCREEN_PAUSE;
	// break;
	// }
	// case DIALOG_GAME_MENU: {
	// guragura.sScreens.peek().state = GuraScreen.SCREEN_PAUSE;
	// break;
	// }
	// default:
	// }
	//
	// }
	//
	// @Override
	// protected Dialog onCreateDialog(int id) {
	// // TODO Auto-generated method stub
	// Dialog dialog;
	// switch (id) {
	// case DIALOG_EXIT: {
	// dialog = exitDialog;
	// break;
	// }
	// case DIALOG_HELP: {
	// dialog = helpDialog;
	// break;
	// }
	// case DIALOG_GAME_MENU: {
	// dialog = gameDialog;
	// break;
	// }
	// default:
	// dialog = null;
	// }
	// return dialog;
	// }
	//
	//
	//

	/*
	 * Kalo mau nampilin menu option di android (non-Javadoc)
	 * 
	 * @see android.app.Activity#onCreateOptionsMenu(android.view.Menu)
	 */
	// @Override
	// public boolean onCreateOptionsMenu(Menu menu) {
	// // TODO Auto-generated method stub
	// if (guragura.gameState == Guragura.MAIN_MENU) {
	// MenuInflater inflater = getMenuInflater();
	// inflater.inflate(R.menu.main_menu, menu);
	// return true;
	// } else {
	// return false;
	// }
	// }

	// @Override
	// public boolean onOptionsItemSelected(MenuItem item) {
	// // TODO Auto-generated method stub
	// switch (item.getItemId()) {
	// case R.id.m_exit_game:
	// showDialog(DIALOG_EXIT);
	// return true;
	// case R.id.m_help_game:
	//
	// showDialog(DIALOG_HELP);
	// return true;
	// default:
	// return super.onOptionsItemSelected(item);
	// }
	// }

	
	/* (non-Javadoc)
	 * @see android.app.Activity#onBackPressed()
	 * override back button behavior demi keberlangsungan game 
	 */
	@Override
	public void onBackPressed() {
		// TODO Auto-generated method stub
		if (guragura.gameState == Guragura.MAIN_MENU) {
			// if (exitDialog.isShowing()) {
			// } else {
			// showDialog(DIALOG_EXIT);
			try {
				guragura.sScreens.peek().exitScreen();
			} catch (Exception e) {
				e.printStackTrace();
				System.out.println(guragura.sScreens.peek());
			}
			// }
		} else if (guragura.gameState == Guragura.GAME) {
			if (guragura.gameScreen.state == GuraScreen.SCREEN_RUNNING) {
				((GameScreen) guragura.gameScreen).pauseGame();
			} else if (guragura.gameScreen.state == GuraScreen.SCREEN_PAUSE) {
				((GameScreen) guragura.gameScreen).resumeGame();
			}

		} else if (guragura.gameState != Guragura.START_SCREEN) {
			guragura.popScreen();
		}
	}

	/**
	 * @author hartono
	 * UI Handler -> {@link Handler} yang diturunkan untuk berkomunikasi dengan thread lainnya
	 */
	public class UIHandler extends Handler {

		public static final int DISPLAY_DIALOG_EXIT = 0;
		public static final int DISPLAY_DIALOG_HELP = 2;
		public static final int DISPLAY_GAME_MENU = 3;

		public static final int DISPLAY_UI_TOAST = 1;

		public UIHandler(Looper looper) {
			super(looper);
		}

		@Override
		public void handleMessage(Message msg) {
			// TODO Auto-generated method stub
			switch (msg.what) {
			case UIHandler.DISPLAY_UI_TOAST: {
				Context context = getApplicationContext();
				Toast t = Toast.makeText(context, (String) msg.obj,
						Toast.LENGTH_LONG);
				t.show();
			}
			case UIHandler.DISPLAY_DIALOG_EXIT: {
				showDialog(DIALOG_EXIT);
				break;
			}
			case UIHandler.DISPLAY_DIALOG_HELP: {
				showDialog(DIALOG_HELP);
				break;
			}
			case UIHandler.DISPLAY_GAME_MENU: {
				showDialog(DIALOG_GAME_MENU);
				break;
			}
			default:
				break;
			}
		}
	}

	@Override
	public void dialogHelp() {
		// TODO Auto-generated method stub
		handler.sendEmptyMessage(UIHandler.DISPLAY_DIALOG_HELP);
	}

	@Override
	public void dialogExit() {
		// TODO Auto-generated method stub
		handler.sendEmptyMessage(UIHandler.DISPLAY_DIALOG_EXIT);
	}

	@Override
	public void dialogGame() {
		handler.sendEmptyMessage(UIHandler.DISPLAY_GAME_MENU);
	}

	@Override
	public void vibrate() {
		// TODO Auto-generated method stub
		if (GuraSettings.vibration) {
			vibrator.vibrate(100);
		}

	}

}